package com.ja.openchat.views;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;

import com.ja.openchat.R;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.weigan.loopview.LoopView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class ChooseReadTimeDialog extends BottomPopupView {
    private OnChooseReadTimeCallback mCallback;
    private int choosePosition = 0;

    public static String[] Burn_After_Read_Array = {"关","5秒","10秒","30秒","1分钟","5分钟","10分钟"};

    public static void show(Context context, OnChooseReadTimeCallback callback,int position){
        new XPopup.Builder(context)
                .hasShadowBg(true)
                .asCustom(new ChooseReadTimeDialog(context,callback,position))
                .show();
    }

    public ChooseReadTimeDialog(@NonNull @NotNull Context context, OnChooseReadTimeCallback callback, int position) {
        super(context);
        mCallback = callback;
        choosePosition = position;
    }



    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_choose_chat_read_time;
    }


    @Override
    protected void onCreate() {
        super.onCreate();
        LoopView loopView = findViewById(R.id.loopView);
        ArrayList<String> list = new ArrayList<>();
        for(String value:Burn_After_Read_Array){
            list.add(value);
        }
        loopView.setItems(list);
        loopView.setInitPosition(choosePosition);
        findViewById(R.id.okBtn).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int index = loopView.getSelectedItem();
                if(mCallback!=null){

                    mCallback.onSuccess(index,list.get(index),getSecondByIndex(index));
                }
                dismiss();
            }
        });
    }

    @Override
    protected int getPopupWidth() {
        return (int) XPopupUtils.getAppWidth(getContext());
    }


    public static int getSecondByIndex(int index){
        int seconds = 0;
        switch (index){
            case 0:
                seconds = 0;
                break;
            case 1:
                seconds = 5;
                break;
            case 2:
                seconds = 10;
                break;
            case 3:
                seconds = 30;
                break;
            case 4:
                seconds = 60;
                break;
            case 5:
                seconds = 300;
                break;
            default:
                seconds = 600;
                break;
        }
        return seconds;
    }
}
